package com.itrip.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itrip.entity.ItripHotelTempStore;
import com.itrip.vo.order.RoomStoreVO;
import com.itrip.vo.store.StoreVO;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.StatementType;

import java.util.Date;
import java.util.List;
import java.util.Map;

public interface ItripHotelTempStoreMapper extends BaseMapper<ItripHotelTempStore>{

    @Select(" SELECT A.roomId,A.recordDate,A.store from (\n" +
            "              SELECT\n" +
            "                    store.roomId,\n" +
            "                    store.recordDate,\n" +
            "                    DATE_FORMAT(store.recordDate,'%Y-%m-%d'),\n" +
            "                   store.store - (\n" +
            "                            CASE\n" +
            "                            WHEN SUM(ord.count) IS NULL THEN\n" +
            "                                0\n" +
            "                            ELSE\n" +
            "                                SUM(ord.count)\n" +
            "                            END\n" +
            "                   ) AS store\n" +
            "                    FROM\n" +
            "                        itrip_hotel_temp_store store\n" +
            "                    LEFT JOIN itrip_hotel_order ord ON store.roomId = ord.roomId AND ord.orderStatus = 0\n" +
            "                    AND DATE_FORMAT(store.recordDate,'%Y-%m-%d') BETWEEN DATE_FORMAT(ord.checkInDate, '%Y-%m-%d') AND DATE_FORMAT(ord.checkOutDate,'%Y-%m-%d')\n" +
            "                    WHERE   store.roomId = #{roomId}\n" +
            "                    GROUP BY  store.roomId,store.recordDate) AS A\n" +
            " WHERE  A.recordDate BETWEEN DATE_FORMAT(#{startTime}, '%Y-%m-%d') AND DATE_FORMAT(#{endTime}, '%Y-%m-%d') ORDER by A.store ASC")
    public List<RoomStoreVO>  queryRoomStore(@Param("roomId") Integer roomId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);


    @Select("call pre_flush_store(#{startTime,mode=IN,jdbcType=DATE},#{endTime,mode=IN,jdbcType=DATE},#{roomId,mode=IN,jdbcType=INTEGER},#{hotelId,mode=IN,jdbcType=INTEGER})")
    @Options(statementType = StatementType.CALLABLE)
    public Map flushStore(Map<String, Object> param);
}
